<?php
namespace Common\Controller;
use Think\Controller;
class BaseController extends Controller{
    public function _initialize(){
        $THEME_PATH = '/tpl/'.MODULE_NAME.'/';
        define('RES', $THEME_PATH . 'common');
        define('STATICS', '/tpl/' . 'static');
    }

    //返回json数据
    public function echo_json_str($state , $message , $url=''){
		/*
		state 返回状态
		message 返回信息
		url 成功后跳转的页面
		*/
		if(empty($url)){
			$url = '';	
		}
		$josn_arr = array(
                        'state' =>   $state,
                        'message' => $message,
                        'url' => $url,
                        ); 
        return json_encode($josn_arr);
	}

	//验证码显示
	public function verify() {
        ob_clean();
		$Verify = new \Think\Verify();        
        $Verify->entry();
	}

	//公用插入数据
	protected function all_add($name = '', $back = '/index'){
        $name = $name ? $name : MODULE_NAME;
        $obj = D($name);
        if ($data = $obj->create()) {
            if ($list = $obj->add($data)) {
                $state = 'SUCCESS';
                $message = L('_action_success');
                $url = $back;
            } else {
                $state = L('_action_fail');
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }

    //公用更新数据
    protected function all_save($name = '', $back = '/index'){        
        $name = $name ? $name : MODULE_NAME;
        $obj = D($name);
        if ($data = $obj->create()) {
            if ($list = $obj->save($data)) {                
                $state = 'SUCCESS';
                $message = L('_action_success');
                $url = $back;
            } else {
                $state = L('_action_fail');
            }            
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }

    //获取聊天信息
    public function getChatMsg(){
        $toID=I("toid");
        $fromID=I("fromid");
        if(empty($toID) || empty($fromID)){
            $ret=array(
                "status"=>"ERROR",
                "msg"=>L("_sys_error")
                );
        }else{
            $endID=S("chatEndID_".$fromID."_".$toID);
            if(empty($endID) || !isset($endID)){
               $endID=0; 
            }
            $where="((toID=".$toID." and fromID=".$fromID.") or (fromID=".$toID." and toID=".$fromID.")) and id>".$endID."";  
            $list=M("Chat")->where($where)->order("id asc")->select();
            if($list){
                foreach ($list as $key => $value) {
                    if($value['toID']==$fromID){
                        M("Chat")->where("id=".$value['id']."")->setField("isread",1);
                    }
                }
                $ret=array(
                    "status"=>"SUCCESS",
                    "data"=>$list
                    );
                S("chatEndID_".$fromID."_".$toID,$list[count($list)-1]['id']);
            }else{
                $ret=array(
                    "status"=>"ERROR",
                    "msg"=>L("_public_noinfo")
                    );
            }
        }
        $this->ajaxReturn($ret);
    }

    
    public function https_post($url,$data = null){
        $ch = curl_init();
        $header = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        if (!$empty) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);       
        return $output;
    }
}